package com.example.tliaswebmangement.controller;

import com.example.tliaswebmangement.pojo.Dept;
import com.example.tliaswebmangement.pojo.Result;
import com.example.tliaswebmangement.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 查询部门数据
     * @return
     */

    @GetMapping
    public Result List(){
        log.info("查询全部部门数据");

        //调用service查询部门数据
        List<Dept> deptList=deptService.list();

        return Result.success(deptList);
    }

    /**
     * 删除部门
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除部门：{}",id);
        deptService.delete(id);
        return Result.success();
    }

    @PostMapping
    public Result insert(@RequestBody Dept dept){
        log.info("新增部门：{}",dept);
        deptService.insert(dept);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result search(@PathVariable Integer id){
        log.info("查询部门:{}",id);
        Dept deptListById=deptService.listById(id);
        return Result.success(deptListById);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门:{}",dept);
        log.info("Received request body: {}", dept);
        deptService.update(dept);
        return Result.success();
    }

}
